package com.hentie.gamelib;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import com.hentie.drawlib.DrawBuffer;
import com.hentie.drawlib.DrawableObject;
import com.hentie.drawlib.OpenGLRenderer;
import com.hentie.drawlib.RenderQueue;
import com.hentie.utils.DeltaTime;

import android.content.Context;
import android.util.Log;

public class Game extends Thread implements RenderQueue{
	
	//The current Context
	Context ctx;
	//The render used to draw
	OpenGLRenderer renderer;
	Queue<DrawableObject> renderQueue = new LinkedList<DrawableObject>();
	
	public float targetDelta = 0.016f;
	boolean isFinished = false;
	//We need a timer
	DeltaTime deltaTime = new DeltaTime();
	float currentDeltaTime;
	
	public boolean IsFixedTimeStep = true;
	public float TimeStep = (1.0f/60.0f);
	
	//The drawing buffer
	List<DrawBuffer> drawBuffer = new ArrayList<DrawBuffer>();

	public Game(Context context, OpenGLRenderer renderer)
	{
		super();
		//Get the context
		this.ctx = context;
		//Set the renderer
		this.renderer = renderer;

		if(renderer == null)
			throw new IllegalArgumentException(); //We need a renderer
		
	}
	
	
	//An empty initialise
	public void Initialize(){}
	
	//An empty Load Contnet
	public void LoadContent(){}
	
	public void Update(float deltaTime){}
	
	public void Draw(OpenGLRenderer renderer, float deltaTime)
	{
		
		if(!renderQueue.isEmpty() && renderer.isReady)
		{

		     //Draw is adding it to the openGL renderer
		     renderer.SetRenderQueue(renderQueue);
		     renderQueue.clear();
		}
		
		
	}
	
	@Override
	public void run()
	{
        while (Thread.currentThread().isInterrupted() == false && !isFinished)
        {
        	if(renderQueue != null)
        	{
        		//renderer.waitDrawingComplete();
		//Update the deltaTime
		deltaTime.Update();
		
		//long deltaTest = deltaTime.getDeltaTime();
		
		//Get the deltatime
		currentDeltaTime = (float) deltaTime.getDeltaTime();
		Update(currentDeltaTime);
		
		if(renderer != null)
		{
			//renderer.waitDrawingComplete();
			Draw(renderer,currentDeltaTime);
		}
        }
        	
        	//Log.v("DeltaTime: ", String.valueOf(currentDeltaTime)+" s");
		//Pass it to the update function
//		Update(currentDeltaTime);
		//Draw
	//	Draw(renderer,currentDeltaTime);
        	//If we hit 60fps then sleep the thread
        	if(currentDeltaTime < targetDelta)
        	{
        		try {
					Thread.sleep((long) (targetDelta*1000f - currentDeltaTime*1000f));
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					//No iggie
				}
        		
        	}
        	
		
        }
	}


	public void AddToRenderQueue(DrawableObject add) {
		// TODO Auto-generated method stub
		

		
		if(renderer != null)
		{
			//Get the objects texture Id
			//add.glTexture = renderer.getTextureResource(add.textureName);
		
		    renderQueue.add(add);
		}
		int bufferPosition = 0;
	    boolean found = false;
		//Now lets search for the right buffer
		for(DrawBuffer buf : drawBuffer)
		{
			if(buf.GetTextureId() == add.glTexture)
			{
				found = true;
				break;
			}
			bufferPosition++;
		}
		
		//If we couldn't find the right buffer
		if(!found)
		{
			//Create a new one
			drawBuffer.add(new DrawBuffer(add.getTextureID(),10));
			//Add to the last buffer
			
		}
		
		
		
		
	}
	

}
